package com.allenkerr.controller;

import com.allenkerr.model.Girl;
import com.allenkerr.dao.GirlRepository;
import com.allenkerr.service.GirlService;
import com.allenkerr.util.ResultUtils;
import com.allenkerr.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by 小柯(Allen Kerr) on 17.6.4 00:07.
 */
@RestController
public class GirlController {

    private static final Logger LOGGER = LoggerFactory.getLogger(GirlController.class);

    @Autowired
    private GirlRepository girlRepository;

    /**
     * 查询所有女生
     */
    @GetMapping("/girls")
    public List<Girl> findAll() {
        List<Girl> girls = girlRepository.findAll();
        return girls;
    }

    /**
     * 保存一个女生
     */
    /*@PostMapping("/girls/save")
    public Girl save(@RequestParam String name, @RequestParam String cupSize, @RequestParam Integer age) {
        Girl girl = new Girl();
        girl.setName(name);
        girl.setAge(age);
        girl.setCupSize(cupSize);
        Girl girlInDb = girlRepository.save(girl);
        return girlInDb;
    }*/
    @PostMapping("/girls/save")
    public Result save(@Valid Girl girl, BindingResult bindingResult) {
        //验证未通过，有错误
        if (bindingResult.hasErrors()) {
            String message = bindingResult.getFieldError().getDefaultMessage();
            //System.out.println(message);
            LOGGER.info("message = {}", message);
            //结束方法，返回错误信息
            return ResultUtils.error(1, message);
        }

        Girl girlInDb = girlRepository.save(girl);
        //成功，携带数据返回
        return ResultUtils.success(girlInDb);
    }

    /**
     * 根据ID查询女生
     */
    @GetMapping("/girls/{id}")
    public Girl findOne(@PathVariable("id") Integer id) {
        /*Girl girl = girlRepository.getOne(id);
        if (girl != null) {
            return JSON.toJSONString(girl);
        }
        return "";*/
        return girlRepository.getOne(id);
    }

    /**
     * 根据ID更新女生
     */
    @PutMapping("/girls/{id}")
    public Girl updateOne(@PathVariable("id") Integer id, @RequestParam String cupSize) {
        Girl girl = girlRepository.getOne(id);
//        Girl girl = new Girl();
//        girl.setId(id);
        girl.setCupSize(cupSize);
        return girlRepository.save(girl);
    }

    /**
     * 根据ID删除女生
     */
    @DeleteMapping("/girls/{id}")
    public void deleteOne(@PathVariable("id") Integer id) {
        girlRepository.delete(id);
    }

    /**
     * 根据年龄查女生
     */
    @GetMapping("/girls/age/{age}")
    public List<Girl> findByAge(@PathVariable("age") Integer age) {
        List<Girl> girls = girlRepository.findByAge(age);
        return girls;
    }

    @Autowired
    private GirlService girlService;

    /**
     * 保存两个女生，测试事务
     */
    @GetMapping("/girls/save/two")
    public List<Girl> saveTwo() {
        Girl girlA = new Girl("苍老师", "E", 28);
        Girl girlB = new Girl("泽老师", "B", 26);
        //调用service
        List<Girl> twoGirls = girlService.saveTwoGirls(girlA, girlB);
        return twoGirls;
    }

    /**
     * 根据id获取女生的年龄，如果小于12岁，返回“上小学”，如果大于12岁并小于16岁，返回“上初中”
     */
    @GetMapping("/girls/getAge/{id}")
    public Result getAgeById(@PathVariable("id") Integer id) throws Exception {
        return girlService.getAgeById(id);
    }

}
